package fr.univsavoie.tatiana2dvisualisation;


import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import fr.emse.tatiana.CorpusNavigationView;
import fr.emse.tatiana.TatianaConstants;
import fr.emse.tatiana.commands.ICommandIds;
import fr.emse.tatiana.display.BadDisplayFile;
import fr.emse.tatiana.display.DisplayFileReader;
import fr.emse.tatiana.replayable.CreatedReplayableInput;
import fr.emse.tatiana.replayable.IReplayableInput;
import fr.emse.tatiana.replayable.ReplayableInput;
import fr.emse.tatiana.replayable.ReplayableModel;
import fr.emse.tatiana.replayable.VisualisationReplayableInput;
import fr.emse.tatiana.scoresheetvisualisation.*;


/**
 * Action for opening the current selection as a score sheet
 * 
 * @author dyke
 *
 */
public class OpenWithEyeTrackerVisualisation extends Action {
	private final IWorkbenchWindow window;
	private final String navigationId;
	private final String editorId;
	

	
	public OpenWithEyeTrackerVisualisation(IWorkbenchWindow window, String label, String editorId, String navigationId) {
		this.window = window;
		this.editorId = editorId;
		this.navigationId = navigationId;
        setText(label);
        // The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_FILTER);
        // Associate the action with a pre-defined command, to allow key bindings.
		//setActionDefinitionId(ICommandIds.CMD_FILTER);
		//setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/sample2.gif"));
		this.setEnabled(false);

	}
	

	
	@Override
	public void run() {
		if(window != null) {	
			try {
				IStructuredSelection sel = ((CorpusNavigationView) window.getActivePage().findView(this.navigationId)).getSelection();
				try {
					IFile replayable = (IFile) sel.getFirstElement();
					if (!replayable.getParent().getName().equals(TatianaConstants.REPLAYABLES_FOLDER_NAME)) {
						return;
					}
					String type = null;
					try {
						DisplayFileReader reader = new DisplayFileReader(replayable.getLocation().toString());
						type = reader.getType();
					} catch (Exception e) {
						// wasn't a display file
					}

					
					IReplayableInput input;
					if (ReplayableInput.DISPLAY_TYPE.equals(type)) {
						input = new ReplayableInput(replayable);
					} else if (ReplayableModel.DISPLAY_TYPE.equals(type)) {
						input = new CreatedReplayableInput(replayable);
					} else {
						return;
					}	
					
					window.getActivePage().openEditor(new VisualisationReplayableInput(replayable, input), editorId);
					
				} catch (ClassCastException e) {
					throw new PartInitException("Not a replayable file selected");
				} catch (BadDisplayFile e) {
					throw new PartInitException("Replayable File could not be opened");
				}
			} catch (PartInitException e) {
				MessageDialog.openError(window.getShell(), "Error", "Error opening view:" + e.getMessage());
			}
		}
	}
}
